;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Barcode.MaintainCode", [])
        .controller("MaintainCodeCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$timeout", "$filter", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $timeout, $filter, UserContextService) {
            
            $scope.handleBlur = function(e) {
                $scope.condition.materialNos = UserContextService.MultiInput(e.target.value);
            };

            $scope.activeFlag = [true];

            $scope.dataList = [];
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };

            $scope.condition = {};
            $scope.reset = function () {
                $scope.condition = {};
            };
            
            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false];
                $scope.activeFlag[index] = true;
                switch (index) {
                    case 0:
                        $scope.detailInfo = {};
                        getDataList();
                        break;
                    default:
                        break;
                }
            };

            $scope.detailInfo = {};

            $scope.editData = function (barcode) {
                var service = "device";
                var reqUrl = "/barcode/item";
                ybHttp.Get(service, reqUrl, {barcode:barcode}).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.changeTab(1);
                    $scope.detailInfo = resp.value;
                    $scope.detailInfo.barcodeFlagShow = resp.value.barcodeFlag ? '分条码' : '总条码';
                })
            };

            $scope.selectSupplier = function () {
                var param = {
                    name: "供货商",
                    type: "supplier"
                };
                ybWidget.SingleSelector(param).then(function (data) {
                    $scope.detailInfo.splirid = data.supplierCode;
                })
            };

            $scope.clearSupplier = function () {
                $scope.detailInfo.splirid = "";
            };

            $scope.updateData = function() {
                var service = "device";
                var reqUrl = "/barcode/update";
                ybHttp.Post(service, reqUrl, $scope.detailInfo).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.changeTab(0);
                })
            };

            $scope.tbConf = {
                fixedWidth: 60,
                scrollWidth: 1200,
                tableCol: [
                    {
                        type: "",
                        name: "零件编号",
                        dataField: "materialsNo",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "零件中文名",
                        dataField: "materialsDes",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "状态",
                        dataField: "statusDesc",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "条码",
                        dataField: "barcode",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "条码类型",
                        dataField: "barcodeFlagShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "条码描述",
                        dataField: "barcodeDesc",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "规格描述",
                        dataField: "model",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "VIN码",
                        dataField: "vinid",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "图号",
                        dataField: "orawyd",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "批次号",
                        width: 100,
                        dataField: "batchId",
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "供货商代码",
                        width: 100,
                        dataField: "splirid",
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 60,
                        isShow: true,
                        isFixed: true,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };

            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false
            };
            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            function getDataList(curPage) {
                var service = "device";
                var reqUrl = "/barcode/list";
                var param = {};
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageSize = $scope.pageInfo.pageSize;
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = $scope.condition;
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    angular.forEach(resp.value.list, function (item) {
                        item.barcodeFlagShow = item.barcodeFlag ? '分条码' : '总条码';
                    })
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.tdClick = function (item, fnType) {
                switch (fnType) {}
            };

            $scope.markInfo = {
                curRow: ''
            };
            
            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    scrollLeft: { left: $scope.tbConf.fixedWidth + "px" }
                };
            };

            (function () {
                $scope.setFixedWidth();
            })();
        }]);
})(window, window.angular);